package com.zaaach.citypicker.adapter;


import com.zaaach.citypicker.ResourceTable;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.util.ScreenUtil;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.List;

/**
 * @Author: Bro0cL
 * @Date: 2018/2/8 21:22
 */
public class GridListAdapter extends BaseItemProvider {
    public static final int SPAN_COUNT = 3;

    private Context mContext;
    private List<HotCity> mData;
    private InnerListener mInnerListener;

    public GridListAdapter(Context context, List<HotCity> data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (mData != null && position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        final City data = mData.get(position);

        DirectionalLayout itemView = new DirectionalLayout(mContext);
        itemView.setOrientation(Component.HORIZONTAL);
        for (int i = 0; i < SPAN_COUNT; i++) {
            int indexPosition = i + position * SPAN_COUNT;
            if (indexPosition >= mData.size()) {
                break;
            }
            Text text = new Text(mContext);
            text.setTextAlignment(TextAlignment.CENTER);
            text.setTextSize(ScreenUtil.sp2px(mContext,15));
            text.setText(mData.get(indexPosition).getName());
            text.setWidth(getScreenWidth(mContext) / SPAN_COUNT);
            text.setHeight(ScreenUtil.dp2px(mContext,40));
            text.setBackground(new ShapeElement(mContext,ResourceTable.Graphic_cp_grid_item_bg));
            text.setMarginsLeftAndRight(15,15);
            text.setMarginsTopAndBottom(20,20);
            text.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (mInnerListener != null){
                        mInnerListener.dismiss(indexPosition, mData.get(indexPosition));
                    }
                }
            });
            itemView.addComponent(text);
        }


//        GridViewHolder holder;
//
//        if (component == null) {
//            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_cp_grid_item_layout, null, false);
//            holder = new GridViewHolder(cpt);
//            // 将获取到的子组件信息绑定到列表项的实例中
//            cpt.setTag(holder);
//        } else {
//            cpt = component;
//            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
//            holder = (GridViewHolder) cpt.getTag();
//        }
//        holder.name.setText(data.getName());


        return itemView;
    }


    public static class GridViewHolder {
        StackLayout container;
        Text name;

        public GridViewHolder(Component itemView) {
            container = (StackLayout) itemView.findComponentById(ResourceTable.Id_cp_grid_item_layout);
            name = (Text) itemView.findComponentById(ResourceTable.Id_cp_gird_item_name);
        }
    }

    public void setInnerListener(InnerListener listener) {
        this.mInnerListener = listener;
    }

    private int getScreenWidth(Context context) {
        return DisplayManager.getInstance().getDefaultDisplay(context).get().getRealAttributes().width-240;
    }
}
